package com.yuuko.yuukoapi.framework.interceptor

import cn.dev33.satoken.stp.StpInterface
import com.yuuko.yuukoapi.repository.system.MenusRepository
import com.yuuko.yuukoapi.repository.system.RolesRepository
import org.springframework.stereotype.Component

@Component
class StpInterfaceInterceptor(
    val rolesRepository: RolesRepository,
    val menusRepository: MenusRepository
) : StpInterface {
    override fun getPermissionList(
        loginId: Any?,
        loginType: String?
    ): List<String?>? {
        val userId = loginId as String?
        if (userId.isNullOrBlank()) {
            return emptyList()
        }
        val authCode = menusRepository.findAllByUserIdAndStatusIsActivated(userId).map { it.authCode }
        return authCode
    }

    override fun getRoleList(
        loginId: Any?,
        loginType: String?
    ): List<String?>? {
        val userId = loginId as String?
        if (userId.isNullOrBlank()) {
            return emptyList()
        }
        val roles = rolesRepository.findAllByUserIdAndAndStatusIsActivated(userId).map { it.name }
        return roles
    }
}